package cn.com.suiyi.shixingshengxian.httputils;

import android.util.Log;

import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.com.suiyi.shixingshengxian.bean.ActiveBean;

import cn.com.suiyi.shixingshengxian.commoditydetail.bean.Commodity;
import cn.com.suiyi.shixingshengxian.home.bean.FlashSale;
import cn.com.suiyi.shixingshengxian.home.bean.HeardPic;
import cn.com.suiyi.shixingshengxian.home.bean.Vegetable;

import cn.com.suiyi.shixingshengxian.bean.MineMessage;
import cn.com.suiyi.shixingshengxian.url.Url;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2016/7/19.
 */
public class HttpUtils {
    private static IRetrofitHttp http;

    private HttpUtils() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(cn.com.suiyi.shixingshengxian.url.Url.BASE_URL).build();
        http = retrofit.create(IRetrofitHttp.class);
    }

    private static HttpUtils httpUtils;

    public static HttpUtils newInstance() {
        if (httpUtils == null) {
            synchronized (HttpUtils.class) {
                if (httpUtils == null) {
                    httpUtils = new HttpUtils();
                }
            }
        }
        return httpUtils;
    }
public  void getActive(){
    Map<String,String> map=new HashMap<>();
    RequestBody body = new MultipartBody.Builder().addFormDataPart("json", "%7B%22os%22%3A%224.4.4%22%2C%22page_size%22%3A10%2C%22device%22%3A%225%22%2C%22pageindex%22%3A1%2C%22api%22%3A%221%22%2C%22deviceid%22%3A%22863092024407994%22%2C%22version%22%3A%223.3.6%22%2C%22size%22%3A640%7D").build();
    map.put("json","%7B%22os%22%3A%224.4.4%22%2C%22page_size%22%3A10%2C%22device%22%3A%225%22%2C%22pageindex%22%3A1%2C%22api%22%3A%221%22%2C%22deviceid%22%3A%22863092024407994%22%2C%22version%22%3A%223.3.6%22%2C%22size%22%3A640%7D");
    Call<ResponseBody> call= http.getActive();
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                Log.i("tag", "onResponse: "+response.body());
                String json = response.body().string();
                ActiveBean activeBean= JSON.parseObject(json,ActiveBean.class);
                EventBus.getDefault().post(activeBean);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });


}
    public  void getMineMessage(){
        Call<ResponseBody> call= http.getMineMessage();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Log.i("tag", "onResponse: "+response.body());
                    String json = response.body().string();
                    MineMessage mineMessage= JSON.parseObject(json,MineMessage.class);
                    EventBus.getDefault().post(mineMessage);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });


    }
    public void getCommodity(String path){

            Call<ResponseBody> call=http.getCommodit("53");
            call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Commodity commodity=JSON.parseObject(response.body().string(),Commodity.class);
                    EventBus.getDefault().post(commodity);
                } catch (IOException e) {
                    EventBus.getDefault().post(null);

                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }
}
